package com.example.proyyj.Controller;

import com.example.proyyj.Entity.Building;
import com.example.proyyj.Entity.ResultEntity;
import com.example.proyyj.Service.BuildingService;
import com.example.proyyj.Util.JSONTool;
import com.example.proyyj.tool.JsonUtil;
import com.example.proyyj.tool.RedisTool;
import org.springframework.boot.SpringApplication;
import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.Jedis;

import javax.annotation.Resource;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/Building/")
public class BuildingController {
    @Resource
    BuildingService buildingService;
    ResultEntity resultEntity=null;
    //修改建筑信息
    @PutMapping("upBuilding")
    private String upBuilding(@RequestParam("Name")String Name,@RequestParam("Img") String Img ,@RequestParam("Era")String Era,@RequestParam("Classify")String Classify,@RequestParam("Region")String Region,@RequestParam("id") int id){
        resultEntity=new ResultEntity();
        try{

            if(buildingService.upBuilding(Name,Img,Era,Classify,Region,id) > 0){
                resultEntity.setCode("0");
                resultEntity.setMsg("修改成功");
            }else {
                resultEntity.setCode("1");
                resultEntity.setMsg("修改失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg("系统错误，请联系管理员");
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }
    //按id查询建筑
    @GetMapping("getBuilding")
    private String getBuildingById(@RequestParam("id")int id){
        resultEntity=new ResultEntity();
        try{
            Building building=buildingService.getBuildingById(id);
            if(building==null){
                resultEntity.setCode("1");
                resultEntity.setMsg("查询失败");
            }else {
                resultEntity.setCode("0");
                resultEntity.setMsg("查询成功");
                resultEntity.setData(building);
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg("系统错误，请联系管理员");
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }

    /**
     * 查询所有建筑
     * @param
     * @return
     */
    @GetMapping("getBuildingList")
    private  String building(){

        try {
            resultEntity=new ResultEntity();
            List<Building> list=buildingService.getBuildingList();
            if (list!=null){
                resultEntity.setCode("0");
                resultEntity.setMsg("查询成功");
                resultEntity.setCount(list.size());
                resultEntity.setData(list);
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode("99999");
            resultEntity.setMsg("查询失败"+e.getMessage());
        }
        return  JSONTool.getJsonTool().toJson(resultEntity);
    }



    @GetMapping("randomBuilding")
    private  String randomBuilding(){//定时刷新
        resultEntity=new ResultEntity();
        try {
            List <Building> list= buildingService.randomBuilding();
            if (list!=null){
                Jedis jedis = new RedisTool().getJedis();
                jedis.set("HotImg",list.get(0).getImg());
                jedis.set("HotImgId",String.valueOf(list.get(0).getId()));
                jedis.set("HotImgName",list.get(0).getBName());
                jedis.set("HotImgInformation",list.get(0).getInformation());
                jedis.set("HotImgEra",list.get(0).getEra());
                jedis.set("HotImgClassify",list.get(0).getClassify());
                jedis.set("HotImgRegion",list.get(0).getRegion());
                resultEntity.setCode("0");
                resultEntity.setMsg("获取成功");
                resultEntity.setData(list);
            }else {
                resultEntity.setCode("1");
                resultEntity.setMsg("获取失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode("99999");
        resultEntity.setMsg("查询失败"+e.getMessage());
    }

        return  JSONTool.getJsonTool().toJson(resultEntity);
    }
     @GetMapping("getImgByRedis")
    private String getImgByRedis(){//前端取值
         resultEntity=new ResultEntity();
         Building building = new Building();
         try{
            resultEntity.setCode("0");
            resultEntity.setMsg("获取成功");
            building.setImg( new RedisTool().getJedis().get("HotImg"));
            building.setId(Integer.parseInt(new RedisTool().getJedis().get("HotImgId")));
            building.setBName(new RedisTool().getJedis().get("HotImgName"));
            building.setInformation(new RedisTool().getJedis().get("HotImgInformation"));
            building.setEra(new RedisTool().getJedis().get("HotImgEra"));
            building.setClassify(new RedisTool().getJedis().get("HotImgClassify"));
            building.setRegion(new RedisTool().getJedis().get("HotImgRegion"));
            resultEntity.setData(building);
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode("99999");
            resultEntity.setMsg("查询失败"+e.getMessage());
        }
         return  JSONTool.getJsonTool().toJson(resultEntity);
     }



}
